package com.sskj.mine.asset;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.hey.lib.HeySpinner;
import com.hey.lib.HeySpinnerBaseAdapter;
import com.hey.lib.HeySpinnerDefaultAdapter;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.CoinUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.BillBean;
import com.sskj.mine.asset.data.CoinTypeBean;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

/**
 * @author Hey
 * Create at  2019/05/04
 */
public class BillActivity extends BaseActivity<BillPresenter> {

    @BindView(R2.id.select_coin)
    HeySpinner selectCoin;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R2.id.select_type)
    HeySpinner selectType;
    @BindView(R2.id.records_list)
    RecyclerView recyclerView;
    private SlimAdapter slimAdapter;
    private String wallType;
    private String pid;
    private Disposable assetDispo;
    private Disposable recordDispo;
    private MVCCoolHelper<List<BillBean>> mvcHelper;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_bill;
    }

    @Override
    public BillPresenter getPresenter() {
        return new BillPresenter();
    }

    HeySpinnerBaseAdapter heySpinnerBaseAdapter = new HeySpinnerBaseAdapter<String>(new ArrayList()) {
        @Override
        protected int getViewId() {
            return R.id.textView;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.mine_item_spinner, null, false);
            }
            ImageView iv = convertView.findViewById(R.id.ivIcon);
            TextView textView = convertView.findViewById(R.id.textView);
            textView.setText(data.get(position));
            switch (position) {
                case 0:
                    iv.setVisibility(View.INVISIBLE);
                    break;
                case 1:
                    iv.setVisibility(View.VISIBLE);
                    iv.setImageResource(R.mipmap.mine_icon_usdt);
                    break;
                case 2:
                    iv.setVisibility(View.VISIBLE);
                    iv.setImageResource(R.mipmap.mine_icon_coin);

                    break;
            }
            return convertView;
        }

        @Override
        protected View bindView(int position, View convertView, ViewGroup parent) {

            return convertView;
        }
    };

    @Override
    public void initView() {

        selectType.setItemGravity(Gravity.CENTER);
        selectCoin.setItemGravity(Gravity.CENTER);
        setTitle(App.INSTANCE.getString(R.string.mine_billActivity1_j));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.mine_item_bill, new SlimInjector<BillBean>() {
            @Override
            public void onInject(BillBean data, IViewInjector injector, List<Object> list) {
                injector
                        .image(R.id.coin_img, data.getPid().equals("1") ? R.mipmap.mine_icon_usdt : R.mipmap.mine_icon_coin)
                        .text(R.id.type_tv, data.getMemo())
                        .text(R.id.num_tv, CoinUtil.keepCoin(data.getPrice()) + (data.getPid().equals("1") ? "USDT" : "GRCT"))
                        .text(R.id.time_tv, data.getAddtime());

            }
        }).attachTo(recyclerView);
        slimAdapter.updateData(new ArrayList());
        ModelRx2DataSource<BillBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<BillBean>)
                page -> mPresenter.getAssetRecordAll(page + "", "20", wallType, pid), 20);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    @Override
    public void initData() {
        mPresenter.getType();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, BillActivity.class);
        context.startActivity(intent);
    }

    public void refresh() {
        mvcHelper.refresh();

    }


    public void updateType(List<CoinTypeBean> assetTypeBeans, Map<String, String> recordStringMap) {

        assetDispo = Flowable.fromIterable(assetTypeBeans)
                .map(coinTypeBean -> coinTypeBean.getPname())
                .toList()
                .subscribe((strings, throwable) -> {
                    strings.add(0, App.INSTANCE.getString(R.string.mine_billActivity2_j));
                    heySpinnerBaseAdapter.setData(strings);
                    selectCoin.setAdapter(heySpinnerBaseAdapter);
                    selectCoin.setSelectIndex(0);
                    selectCoin.setOnSelectListener(position -> {
                        if (position == 0) {
                            pid = null;
                        } else {
                            pid = assetTypeBeans.get(position - 1).getPid();
                        }
                        refresh();
                    });

                });
        recordDispo = Flowable.fromIterable(recordStringMap.values())
                .toList()
                .subscribe((strings, throwable) -> {
                    strings.add(0, App.INSTANCE.getString(R.string.mine_billActivity2_j));
                    selectType.attachData(strings);
                    selectType.setOnSelectListener(position -> {
                        Iterator<Map.Entry<String, String>> iterator = recordStringMap.entrySet().iterator();
                        wallType = null;
                        while (iterator.hasNext()) {
                            Map.Entry<String, String> next = iterator.next();
                            if (next.getValue().equals(strings.get(position))) {
                                wallType = next.getKey();
                            }
                        }
                        refresh();
                    });

                });
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(assetDispo);
        DisposUtil.close(recordDispo);
        super.onDestroy();
    }

    public void updateUI(List<BillBean> list) {
        slimAdapter.updateData(list);
    }
}
